<?php
namespace src;
use classlib\input;
use src\logic\Cadmin;
use classlib\core as C;
use classlib\core\controller;
class Crole extends controller {
	public function main() {
		$view   = C\view::inst('extjs');
		$userid = Cadmin::chkaccess($view);
		$view->assign_by_ref('title', Cadmin::$name);
		$view->display('core/role.ext.js');
	}

	public function dolist() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		list($start, $limit, $sort) = Cadmin::offset();
		$where  = array();
		$name   = input::post('name');
		if ($name) {
			$where[] = array('name', '%'.$name.'%', \classlib\orm::OP_LIKE);
		}
		$orm    = C\model::load('main.sys.role');
		$total  = $orm->get_total($where);
		$list   = array();
		if ($total) {
			$list   = $orm->get_list($start, $limit, $where, '*', $sort);
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}
	
	/**
	 * @desc    删除系统配置的角色信息
	 * @doc     删除角色
	 * @access  需要授权
	 */
	public function doremove() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$id     = (int)input::post('id');
		//验证是否有人在使用
		$where  = array('ruleid'=>$id);
		$total = C\model::load('main.sys.ruser')->get_total($where);
		if ($total > 0) {
			$lang = C\lang::inst('adm');
			$view->assign_error('1', $lang->get('common', 40020));
		}
		C\model::load('main.sys.access')->get_orm()->where('ruleid', $id)->delete();
		C\model::load('main.sys.access')->set_ver();
		C\model::load('main.sys.role')->delete($id);
		$view->display();
	}
	
	/**
	 * @desc    创建配置的角色信息
	 * @doc     创建角色
	 * @access  需要授权
	 */
	public function dosave() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$id     = (int)input::post('id');
		$data   = array();
		$lang   = C\lang::inst('adm');
		$data['name'] = trim(input::post('name'));
		if (!$data['name']) {
			$view->assign_error('name', $lang->get('common', 40021));
		}
		//验证角色名是否存在
		$where  = array('name'=>$data['name']);
		$oid = C\model::load('main.sys.role')->isexist($where);
		if ($oid > 0 && $oid != $id) {
			$view->assign_error('name', $lang->get('common', 40022));
		}
		$data['remark']  = input::post('remark');
		if($id < 1) {
			C\model::load('main.sys.role')->new_one($data);
		} else {
			C\model::load('main.sys.role')->save($id, $data);
		}
		$view->display();
	}

	/**
	 * @desc    给角色授权
	 * @doc     角色授权
	 * @access  需要授权
	 */
	public function setaccess() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$lang   = C\lang::inst('adm');
		$id     = (int)input::post('id');
		if ($id < 1) {
			$view->assign_error('1', $lang->get('common', 40023));
		}
		$role   = C\model::load('main.sys.role')->get_one($id);
		if(empty($role)) {
			$view->assign_error('2', $lang->get('common', 40024));
		}
		C\model::load('main.sys.access')->get_orm()->where('ruleid', $id)->delete();
		$access = input::post('access');
		if(!empty($access)) {
			$access = explode(',', $access);
			$access = array_filter($access, 'is_numeric');
			foreach ($access as $mid) {
				$data = array('ruleid'=>$id, 'menuid'=>$mid);
				C\model::load('main.sys.access')->new_one($data);
			}
		}
		$view->assign('msg', $lang->get('common', 40025));
		$view->display();
	}
	
	public function doaccess() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$id     = (int)input::post('id');
		$where  = array('ruleid'=>$id);
		$access = C\model::load('main.sys.access')->get_column(0, -1, $where, 'menuid');
		if(!is_array($access)) {
			$access = array();
		}
		$view->assign_by_ref('access', $access);
		$view->display();
	}
}